package com.hui.server.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;


import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static java.util.Collections.singletonList;

/**
 * @BelongsProject: yeb
 * @BelongsPackage: com.hui.server.config.swagger
 * @Author: HUI
 * @CreateTime: 2022-04-04 10:56
 * @Description: Swagger配置全局统一认证：https://www.jianshu.com/p/16408f2bfc06
 */


//访问：http://localhost:9991/swagger-ui/index.html#/
@Configuration
@EnableOpenApi //开启 swagger2
@EnableWebMvc
public class Swagger2Config   {
    private static final String TOKEN_HEADER = "Authorization";


    @Bean
    public Docket docket() {

        //Collections.singletonList()  创建只有一个元素的集合
        List<SecurityScheme> securitySchemes =  Collections.singletonList(new ApiKey(TOKEN_HEADER, TOKEN_HEADER, "header"));
        List<SecurityContext> securityContexts = Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(securityReferences())
                        .operationSelector(operationContext -> true)
                        .build()
        );



        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //坑：将LocalDate[].class转化String[].class 为了能够在swagger的页面中正确识别，傻逼swagger
//                .directModelSubstitute(LocalDate[].class, Date[].class)
                .select()
                //配置扫描接口
                .apis(RequestHandlerSelectors.basePackage("com.hui.server.controller"))
                //配置扫描所有路径
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(securitySchemes)
                .securityContexts(securityContexts);



    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Hui-云E办接口文档")
                .description("Hui-云E办接口文档")
                .contact(new Contact("Hui", "http://localhost:9991/swagger-ui/index.html#", "xxxx@xxxx.com"))
                .version("1.0")
                .build();
    }

    public List<SecurityReference> securityReferences() {
        AuthorizationScope[] authorizationScopes =
                new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")};

        SecurityReference reference = new SecurityReference(TOKEN_HEADER, authorizationScopes);
        return singletonList(reference);
    }


}



 /*class SwaggerConfig {

    private static final String TOKEN_HEADER = "token";

    @Bean
    public Docket petApi() {

        //Collections.singletonList()  创建只有一个元素的集合
        List<SecurityScheme> securitySchemes =  Collections.singletonList(new ApiKey(TOKEN_HEADER, TOKEN_HEADER, "header"));
        List<SecurityContext> securityContexts = Collections.singletonList(
                SecurityContext.builder()
                        .securityReferences(securityReferences())
                        .operationSelector(operationContext -> true)
                        .build()
        );

        ApiInfo apiInfo = new ApiInfoBuilder()
                .title("fly admin api")
                .contact(new Contact("guoxiang", "null", "xxx@163.com"))
                .version("1.0")
                .build();

        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo)
                .securitySchemes(securitySchemes)
                .securityContexts(securityContexts)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.fly.admin"))
                .build();
    }

    public List<SecurityReference> securityReferences() {
        AuthorizationScope[] authorizationScopes =
                new AuthorizationScope[]{new AuthorizationScope("global", "accessEverything")};

        SecurityReference reference = new SecurityReference(TOKEN_HEADER, authorizationScopes);
        return singletonList(reference);
    }

}
*/